#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define A_MEGABYTE (1024 * 1024)
#define PHY_MEM_MEGS 1024

int main()
{
	char *some_memory;
	size_t size_to_allocate = A_MEGABYTE;
	int megs_obtained = 0;

	while (megs_obtained < (PHY_MEM_MEGS * 2))
	{													// 2048
		some_memory = (char *)malloc(size_to_allocate); // malloc函数返回值是一个void *指针，需要类型转换成我们需要的char *类型
		if (some_memory != NULL)
		{
			megs_obtained++;
			sprintf(some_memory, "Hello World!");
			printf("%s - now allocated %d Megabytes\n", some_memory, megs_obtained);
		}
		else
		{
			exit(EXIT_FAILURE);
		}
	}
	exit(EXIT_SUCCESS);
}
